home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / edit / pt20pc.zip / PIECE.C < prev    next >
C/C++ Source or Header  |  1991-02-04  |  6KB  |  255 lines

  1. #include "pt.h"
  2. #include "malloc.h"
  3.  
  4. struct piece * pascal
  5. /* XTAG:dupPieces */
  6. dupPieces(pp)
  7.     register struct piece *pp;
  8. {
  9.     register struct piece *pp2;
  10.     struct piece *lastpp, *retpp;
  11.  
  12.     lastpp = retpp = NULL;
  13.     while( pp != NULL ) {
  14.         pp2 = getFreePiece();
  15.         /* link to the previous piece */
  16.         if( lastpp != NULL )
  17.             lastpp->nextPiece = pp2;
  18.         else    /* first time through, remember the first piece */
  19.             retpp = pp2;
  20.         pp2->prevPiece = lastpp;
  21.         /* copy the field values */
  22.         pp2->file = pp->file;
  23.         pp2->position = pp->position;
  24.         pp2->length = pp->length;
  25.         lastpp = pp2;
  26.         pp = pp->nextPiece;
  27.     }
  28.     /* pp2->nextPiece == NULL already since getFreePiece does that */
  29.     return retpp;
  30. }
  31.  
  32. struct piece * pascal
  33. /* XTAG:getFreePiece */
  34. getFreePiece()
  35. {
  36.     extern unsigned char msgBuffer[];
  37.     extern struct piece *freePList;
  38.     extern unsigned char *userMessages[];
  39.     extern unsigned int bytesLeft;
  40.     extern unsigned int piecesLeft;
  41.     extern int debug;
  42.     
  43.     register struct piece *pp;
  44.     /* start this at six since replace in search.c will issue the */
  45.     /* message when it occurs in a replace (the most likely case) */
  46.     /* six times will use 6*16 = 96 bytes. */
  47.     static int errorMsgCount = 6;
  48.     
  49.     pp = freePList;
  50.     if( pp != NULL ) {
  51.         --piecesLeft;
  52.         freePList = freePList->nextPiece;
  53.         pp->nextPiece = pp->prevPiece = NULL;
  54.         return pp;
  55.     }
  56.     /* else we have to allocate a piece structure from the free space */
  57.     pp = (struct piece *)malloc(sizeof(struct piece));
  58.     bytesLeft = _memavl();
  59.     if( bytesLeft < SPACELOW ) {
  60.         if( --errorMsgCount <= 0 ) {
  61.             msg(userMessages[LOWSPACEMSG], 3);
  62.             errorMsgCount = 9;
  63.         }
  64.     }
  65.     pp->nextPiece = pp->prevPiece = NULL;
  66.     return pp;
  67. }
  68.  
  69. void pascal
  70. /* XTAG:freePieces */
  71. freePieces(pp)
  72.     struct piece *pp;
  73. {
  74.     extern struct piece *freePList;
  75.     extern unsigned int piecesLeft;
  76.     
  77.     register struct piece *pp2;
  78.     
  79.     if( pp == NULL )
  80.         return;
  81.     pp2 = pp;
  82.     while( pp2->nextPiece != NULL ) {
  83.         pp2 = pp2->nextPiece;
  84.         ++piecesLeft;
  85.     }
  86.     pp2->nextPiece = freePList;
  87.     freePList = pp;
  88.     ++piecesLeft;
  89. }
  90.  
  91. struct piece * pascal
  92. /* XTAG:findPiece */
  93. findPiece(logPos, ff, beginLogPos)
  94.     long logPos, *beginLogPos;
  95.     register struct openFile *ff;
  96. {
  97.     extern unsigned char msgBuffer[];
  98.  
  99.     register struct piece *pp;
  100.     long nn, n2;
  101.  
  102.     /* see if we already know what piece it is in */
  103.     if( ff->loLogPiece <= logPos && logPos <= ff->hiLogPiece ) {
  104.         pp = ff->logPiece;
  105.         nn = ff->loLogPiece;
  106.         /* now nn = first logical byte in this piece */
  107.     } else {    /* go through the piece table */
  108.         pp = ff->logPiece;
  109.         nn = ff->loLogPiece;
  110.         if( logPos < nn ) {    /* below this piece? */
  111.             if( logPos < 0 ) {  /* error checking to be safe */
  112.                 msg("findPiece: byte number < 0", 3);
  113.                 return pp;    /* what else? */
  114.             }
  115.             /* search down for the piece */
  116.             while( logPos < nn ) {
  117.                 pp = pp->prevPiece;
  118.                 nn -= pp->length;
  119.             }
  120.         } else {    /* must be at or above this piece */
  121.             if( logPos >= ff->fileSize ) {
  122.                 /* return the last piece */
  123.                 while( pp->nextPiece != NULL ) {
  124.                     nn += pp->length;
  125.                     pp = pp->nextPiece;
  126.                 }
  127.             } else {
  128.                 /* search up for the piece */
  129.                 while( 1 ) {
  130.                     n2 = nn + pp->length;
  131.                     if( logPos < n2 )
  132.                         break;
  133.                     nn = n2;
  134.                     pp = pp->nextPiece;
  135.                 }
  136.             }
  137.         }
  138.     }
  139.     *beginLogPos = nn;
  140.     return pp;
  141. }
  142.  
  143. void pascal
  144. /* XTAG:hidePiece */
  145. hidePiece(beginLogPos)
  146.     long beginLogPos;
  147. {
  148.     extern unsigned char msgBuffer[];
  149.     extern struct window *selWindow;
  150.     extern struct openFile *files;
  151.     extern int debug;
  152.  
  153.     register struct piece *pp;
  154.     register struct openFile *ff;
  155.     long nn;
  156.  
  157.     /* NOTE: This code implies that the piece begin hidden */
  158.     /* MUST be in the selection window */
  159.     ff = &files[selWindow->fileId];
  160.     pp = findPiece(beginLogPos, ff, &nn);
  161.     if( beginLogPos != nn ) {
  162.         sprintf(msgBuffer, "hidePiece: pos=%ld beginPiece=%ld",
  163.             beginLogPos, nn);
  164.         msg(msgBuffer, 3);
  165.         return;
  166.     }
  167.  
  168.     /* Now we fix up the piece to be of zero length */
  169.     /* First subtract the length we are zeroing from the file size */
  170.     ff->fileSize -= pp->length;
  171.     
  172.     /* we have to recompute posBotline so incorrect values do */
  173.     /* not mess other things up */
  174.     if( selWindow->posTopline <= beginLogPos
  175.      && beginLogPos < selWindow->posBotline )
  176.         selWindow->posBotline -= pp->length;
  177.  
  178.     /* Then zero the length of the piece.  Now the contents of the */
  179.     /* piece are still in the add file but the piece will not show */
  180.     /* up in the file itself. */
  181.     pp->length = 0L;
  182.  
  183.     /* Make the file handle a special value for hidden pieces */
  184.     pp->file = 999;
  185.  
  186.     /* now we have to make sure the caches are correct */
  187.     ff->hiLogBuffer = -1L;    /* invalidate the buffer cache */
  188.     /* Make the first piece in the file the cached piece */
  189.     ff->logPiece = ff->pieceList;
  190.     ff->loLogPiece = 0L;    /* first piece is at logical offset 0 */
  191.     ff->hiLogPiece = ff->logPiece->length;
  192. }
  193.  
  194. int pascal
  195. /* XTAG:deleteHiddenText */
  196. deleteHiddenText(w)
  197.     struct window *w;
  198. {
  199.     extern unsigned char msgBuffer[];
  200.     extern struct openFile *files;
  201.  
  202.     register struct piece *pp;
  203.     struct piece *ppNext, *ppPrev;
  204.  
  205.     pp = files[w->fileId].pieceList;
  206.     while( pp != NULL ) {
  207.         ppNext = pp->nextPiece;
  208.         if( pp->file == 999 ) {
  209.             ppPrev = pp->prevPiece;
  210.             if( ppPrev != NULL )
  211.                 ppPrev->nextPiece = ppNext;
  212.             if( ppNext != NULL )
  213.                 ppNext->prevPiece = ppPrev;
  214.             pp->nextPiece = NULL;
  215.             /* freePieces only looks at nextPiece */
  216.             /* (not prevPiece) so we only NULL nextPiece */
  217.             freePieces(pp);
  218.         }
  219.         pp = ppNext;
  220.     }
  221.     return 0;
  222. }
  223.  
  224. long pascal
  225. /* XTAG:findHiddenPiece */
  226. findHiddenPiece(w, startPos, offset)
  227.     struct window *w;
  228.     long startPos, *offset;
  229. {
  230.     extern unsigned char msgBuffer[];
  231.     extern struct openFile *files;
  232.  
  233.     register struct piece *pp;
  234.     struct openFile *ff;
  235.     long beginPos;
  236.  
  237.     ff = &files[w->fileId];
  238.     /* We have to treat startPos=0 as a special case since hidden */
  239.     /* pieces have zero length and the piece after them will be found */
  240.     /* first if a hidden piece is first in the piece list */
  241.     if( startPos == 0L ) {
  242.         beginPos = 0L;
  243.         pp = ff->pieceList;
  244.     } else
  245.         pp = findPiece(startPos, ff, &beginPos);
  246.     while( pp != NULL && pp->file != 999 ) {
  247.         beginPos += pp->length;
  248.         pp = pp->nextPiece;
  249.     }
  250.     if( pp == NULL )
  251.         return -1L;
  252.     *offset = pp->position;
  253.     return beginPos;
  254. }
  255.